<?php
/**[规则逻辑层模型]
 * @Author: chenli
 * @Email:  976123967@qq.com
 * @Date:   2015-02-23 16:49:11
 * @Last Modified by:   chenli
 * @Last Modified time: 2015-02-28 23:11:49
 */
class AuthRuleLogic extends Model{
	public $tableName = 'auth_rule';
	private $cache;
	public function _initialize()
	{	
		$this->cache = S('authrule');
	}


	public function find_all($isArray=0)
	{

		if($this->cache)
		{
			foreach($this->cache as $k=>$v)
			{
				$this->cache[$k]['status_name'] = $v['status']?'启用':'禁用';
				$this->cache[$k]['isnavshow_name'] = $v['isnavshow']?'显示':'隐藏';
			}
		}


		if($isArray)
			return Data::channelLevel($this->cache,0,'','id','pid');
		else
			return Data::tree($this->cache,'title','id','pid');
	}



	/**
	 * [update_cache 更新缓存]
	 * @return [type] [description]
	 */
	public function update_cache()
	{
		$data = $this->order(array('sort'=>'asc'))->select();
		$temp = array();
		if($data) 
		{
			foreach($data as $k=>$v)
			{
				$temp[$v['id']] = $v;
			}
		}

		S('authrule',$temp);
	}

	/**
	 * [add_auth_rule 添加规则]
	 */
	public function add_auth_rule()
	{
		if(!$this->create())
			return false;
		$id = $this->add();
		$this->update_cache();
		return $id;
	}

	// 自动验证
	protected $_validate = array(
		array('name','require','请输入规则标识',1),
		array('name','check_name','规则标识已经存在',1,'callback'),
		array('title','require','请输入规则名称',1),
		array('title','check_title','规则名称已经存在',1,'callback'),
	);

	/**
	 * [check_name 验证规则标识是否存在]
	 * @param  [type] $con [description]
	 * @return [type]      [description]
	 */
	protected function check_name($con)
	{
		$id  = I('post.id');
		if($id)
			$where['id'] = array('neq',$id);
		$where['name'] = $con;
		$data = $this->where($where)->find();
		if($data)
			return false;
		return true;
	}

	/**
	 * [check_title 验证规则名称是否存在]
	 * @param  [type] $con [description]
	 * @return [type]      [description]
	 */
	protected function check_title($con)
	{
		$id  = I('post.id');
		if($id)
			$where['id'] = array('neq',$id);
		$where['title'] = $con;
		$data = $this->where($where)->find();
		if($data)
			return false;
		return true;
	}


	// 自动完成
	public $_auto = array(

		array('level','_level',3,'callback'),
	);

	/**
	 * [_level 级别自动完成]
	 * @return [type] [description]
	 */
	protected function _level()
	{
		$pid = I('post.pid');
		if($pid==0)
			return 1;
		else
		{
			$data = $this->where(array('id'=>$pid))->find();
			return $data['level']+1;
		}

	}

	/**
	 * [find_one 查找一条记录]
	 * @return [type] [description]
	 */
	public function find_one()
	{
		$id = I('get.id');
		if(!$id)
		{
			$this->error = '链接错误';
			return false;
		}
		$data = $this->where(array('id'=>$id))->find();
		if(!$data)
		{
			$this->error = '规则不存在';
			return false;
		}
		return $data;
	}

	/**
	 * [edit_auth_rule 编辑]
	 * @return [type] [description]
	 */
	public function edit_auth_rule()
	{
		if(!$this->create())
			return false;
		$this->save();
		$this->update_cache();
		return true;
	}

	/**
	 * [del_auth_rule 删除规则]
	 * @return [type] [description]
	 */
	public function del_auth_rule()
	{
		$id = I('get.id');
		if(!$id)
		{
			$this->error = '链接错误';
			return false;
		}
		$data = $this->where(array('id'=>$id))->find();
		if(!$data)
		{
			$this->error = '规则不存在';
			return false;
		}
		$this->delete($id);
		$this->update_cache();
		return true;
	}

	/**
	 * [operation 处理批量操作]
	 * @return [type] [description]
	 */
	public function operation()
	{
		$id  = I('post.id');
		switch (true) 
		{
			// 排序
			case isset($_POST['update_sort']):
				$sort = I('post.sort');
				foreach($id as $k=>$v)
				{
					$this->save(array('sort'=>$sort[$k],'id'=>$v));
				}
				$msg= '排序成功';
				break;
			default:
				$this->error = '操作选择错误';
				return false;

		}
		// 更新缓存
		$this->update_cache();
		return $msg;
	}


}